import { defineStore } from 'pinia'

export const useStore = defineStore('main', {
  state: () => ({
    user: {
      isAuthenticated: false,
      username: ''
    }
  }),
  actions: {
    login(username) {
      this.user.isAuthenticated = true
      this.user.username = username
      localStorage.setItem('user', JSON.stringify(this.user))
    },
    logout() {
      this.user.isAuthenticated = false
      this.user.username = ''
      localStorage.removeItem('user')
    },
    initialize() {
      const savedUser = localStorage.getItem('user')
      if (savedUser) {
        this.user = JSON.parse(savedUser)
      }
    }
  }
})